var app = getApp();

import api from '../../../utils/api.js'
var app = getApp();
var QQMapWX = require("../../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
import { HTTP } from '../../../utils/http.js'
import utils from '../../../utils/util.js'
import { config } from '../../../utils/config'

let http = new HTTP()

Page({
	data: {
		imgUrl: config.imgUrl,
		vegelist: [],
		page: 1,
		searchObj: {
			searchBg: "#fff",
			placeStr: '请输入菜品名称'
		}

	},
	onLoad: function (options) {
		let id = options.id
		wx.setNavigationBarTitle({
			title: options.name,
		});
		this.setData({ id })
		this.shopCateProducts();
	},
	shopCateProducts() {
		let address = wx.getStorageSync('address');
		let data = {
			lat: address.latitude,
			lng: address.longitude,
			id: this.data.id,
			page: this.data.page
		}
		this.getInfo(api.shopCateProducts, data).then(res => {
			console.log(res);
			this.setData({
				last_page: res.last_page,
				vegelist: this.data.vegelist.concat(res.data)
			})

		})
	},
	/*获取*/
	getInfo(url, data) {
		return new Promise((resolve, reject) => {
			wx.showLoading({
				title: '加载中...',
			})
			http.request({
				url: url,
				method: "GET",
				data: data,
				success: res => {
					if (res.msg && res.error_code) {
						wx.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1500
						});
						return;
					}
					if (res.code == 200) {
						resolve(res.data);
					}
					wx.hideLoading()
				},
				fail: () => { },
				complete: () => {
					wx.hideLoading()
				}


			})
		})
	},
	/*搜索*/
	searchInput: utils.debounce(function (e) {
		let inputValue = e[0].detail.value;
		if (inputValue) {
			this.setData({
				inputValue,
				deleteBtn: true
			})
		} else {
			this.setData({
				deleteBtn: false
			})
		}

	}, 200),
	/*删除搜索框内容*/
	deleteSearch() {
		console.log(1);

		this.setData({
			inputValue: '',
			deleteBtn: false
		})
	},
	/*搜索按钮*/
	searchHandle(e) {

		let inputValue = e.detail.inputValue
		this.setData({
			inputValue
		})

		wx.navigateTo({
			url: '/pages/classify/searchVege/searchVege?kw=' + inputValue + '&id=' + this.data.id
		})

	},
	addBasket(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/classify/addBasket/addBasket?id=' + id
		})
	},
	onReachBottom: function () {
		console.log('触底刷新');
		if (this.data.page < this.data.last_page) {
			this.setData({
				page: this.data.page + 1
			})
			this.shopCateProducts();
		}
	},



})